import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

const regionFields = ['provincesCode', 'citiesCode', 'areasCode', 'streetsCode', 'villagesCode'];
export default class PartnerForm extends Base {
  constructor(partner) {
    super();

    if (partner === undefined) {
      partner = {
        status: 1
      };
      this.regionData = [];
      this.majorTypeId = '';
      this.branchId = '';
      this.partnerLicense = [];
      this.partnerHeadImg = [];
    } else {
      this.partnerId = partner.partnerId;
      this.branchId = partner.branchId;
      this.majorBranchPartnerId = partner.majorBranchPartnerId;

      let majorTypeId = partner.majorTypeId;
      if (SystemUtil.isEmpty(majorTypeId)) {
        majorTypeId = '';
      }
      this.majorTypeId = majorTypeId;

      let partnerLicense = partner.partnerLicense;
      if (SystemUtil.isEmpty(partnerLicense)) {
        partnerLicense = [];
      } else {
        partnerLicense = [partnerLicense];
      }
      this.partnerLicense = partnerLicense;

      let partnerHeadImg = partner.partnerHeadImg;
      if (SystemUtil.isEmpty(partnerHeadImg)) {
        partnerHeadImg = [];
      } else {
        partnerHeadImg = [partnerHeadImg];
      }
      this.partnerHeadImg = partnerHeadImg;

      let regionData = [];
      for (let i = 0; i < regionFields.length; i++) {
        const field = regionFields[i];
        let value = partner[field];
        if (!SystemUtil.isEmpty(value)) {
          regionData.push(value);
        }
      }
      this.regionData = regionData;
    }
    let fields = ['branchName', 'partnerName', 'status', 'partnerPhone', 'partnerAddress', 'partnerBankNo', 'partnerBankInfo',
      'partnerWechatMchId', 'majorName'
    ];
    this.initObject(partner, fields);

    let lng = partner.longitude;
    let lat = partner.latitude;
    let position;
    if (SystemUtil.isEmpty(lng) || SystemUtil.isEmpty(lat)) {
      position = [];
    } else {
      lat = Number(lat);
      lng = Number(lng);
      position = [lat, lng];
    }
    this.position = position;
  }

  checkValid() {
    let fields = ['partnerName', 'partnerHeadImg', 'branchId', 'majorTypeId', 'partnerPhone', 'position', 'partnerAddress', 'regionData',
      'partnerLicense', 'partnerBankNo', 'partnerBankInfo', 'partnerWechatMchId'
    ];

    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let partnerBankNo = this.partnerBankNo;
    let reg = /^[1-9]\d{12,18}$/;
    if (!reg.test(partnerBankNo)) {
      return 'bankNoNotTest';
    }
    return '';
  }

  getObject() {
    let postData = {};
    let fields = ['partnerName', 'majorTypeId', 'branchId', 'partnerPhone', 'partnerAddress', 'partnerBankNo', 'partnerBankInfo',
      'partnerWechatMchId', 'status'
    ];
    this.setPostData(postData, fields, true);

    let position = this.position;
    let [lat, lng] = position;
    postData.longitude = lng;
    postData.latitude = lat;

    let regionData = this.regionData;
    for (let i = 0; i < regionFields.length; i++) {
      const field = regionFields[i];
      let value = regionData[i];
      postData[field] = value;
    }

    let partnerLicense = this.partnerLicense;
    postData.partnerLicense = partnerLicense[0];

    let partnerHeadImg = this.partnerHeadImg;
    postData.partnerHeadImg = partnerHeadImg[0];

    let partnerId = this.partnerId;
    if (partnerId !== undefined) {
      postData.partnerId = partnerId;
      postData.majorBranchPartnerId = this.majorBranchPartnerId;
    }
    return postData;
  }
}
